package com.sczmgk.syevaluation.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.text.TextUtils;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.sczmgk.syevaluation.R;
import com.sczmgk.syevaluation.utils.Tools;

/**
 * 生成DiaLog的工厂类
 * 
 * @author
 * 
 */
public class DialogFactory {
	private static final String TAG = DialogFactory.class.getSimpleName();

	/**
	 * 取得进度打的DiaLog
	 * 
	 * @param context
	 * @param tip
	 * @return
	 */
	public static Dialog getProgressDislog(final Context context, String tip) {

		final Dialog dialog = new Dialog(context, R.style.style_progress_dialog);
		dialog.setContentView(R.layout.dialog_layout);
		// dialog.setCancelable(true);
		Window window = dialog.getWindow();
		WindowManager.LayoutParams lp = window.getAttributes();
		int width = Tools.getScreenWidth(context);
		lp.width = (int) (0.6 * width);

		TextView titleTxtv = (TextView) dialog.findViewById(R.id.tvLoad);
		if (tip == null || tip.length() == 0) {
			titleTxtv.setText(R.string.dialog_default_text);
		} else {
			titleTxtv.setText(tip);
		}

		return dialog;
	}


	/**
	 * 生成正在加载的Dialog
	 * 
	 * @param context
	 * @param resId
	 *            提示内容引用
	 */
	public static Dialog getLoadingDialog(final Context context, int resId) {
		Dialog loadingDialog = new Dialog(context, R.style.style_progress_dialog);
//		loadingDialog.setContentView(R.layout.dialog_loading_layout);
		loadingDialog.setContentView(R.layout.dialog_loading_layout);
		loadingDialog.setCancelable(true);
		Window window = loadingDialog.getWindow();
		WindowManager.LayoutParams lp = window.getAttributes();
		int width = Tools.getScreenWidth(context);
		lp.width = (int) (0.15 * width);//设置dialog的宽度
		lp.height = (int) (0.15 * width);//设置dialog的高度
		TextView titleTxtv = (TextView) loadingDialog.findViewById(R.id.tv_loading);
		loadingDialog.setCanceledOnTouchOutside(false);
		String tip = context.getResources().getString(resId);
		if (tip != null) {
			titleTxtv.setText(tip);
		}
		return loadingDialog;
	}

	/**
	 * 显示内容的对话框
	 * 
	 * @param ctx
	 * @param neutralListener
	 * @param negativeListener
	 * @param message
	 * @param neutralText
	 * @param negativeText
	 * @return
	 */
	public static Dialog showContentDialog(Context ctx,
										   OnClickListener neutralListener, OnClickListener negativeListener,
										   String message, String neutralText, String negativeText) {
		MessageDialog msgDialog = new MessageDialog(ctx, true);

		msgDialog.setMessage(message);
		msgDialog.setBtn1ClickListener(neutralListener);
		msgDialog.setBtn1Text(neutralText);

		msgDialog.setBtn2ClickListener(negativeListener);
		msgDialog.setBtn2Text(negativeText);

		msgDialog.setBtn3Visible(false);

		msgDialog.setBtn4Visible(false);

		msgDialog.show();

		return msgDialog;
	}

	/******************* 只有确定的Dialog *************************/

	
	
	/**
	 * @param context
	 *            上下文
	 * @param  title           
	 * @param message
	 *            提示信息
	 * @param sureString
	 *            确定按钮文字
	 * @param surelistener
	 *            确定按钮监听事件
	 */
	public static void warningDialog(Context context, String title , String message,
									 String sureString, OnClickListener surelistener) {
		if (null == context) {
			return;
		}
		if (context instanceof Activity) {
			Activity activity = (Activity) context;
			if (activity.isFinishing()) {
				return;
			}
		}
		MessageDialog msgDialog = new MessageDialog(context);
		msgDialog.setTitle(title);
		msgDialog.setTitleTextColor(context.getResources().getColor(R.color.blue_common_color));
		msgDialog.setOneBtnStyle();
		msgDialog.setBtn4Enable(true);
		msgDialog.setMessage(message);
		msgDialog.setBtn4Text(sureString);
	    msgDialog.setOneBtnClickListener(surelistener);
		if (!TextUtils.isEmpty(sureString)) {
			msgDialog.setBtn4Text(sureString);
		}
		msgDialog.show();
	}

	/**
	 * @param context      上下文
	 * @param strId        提示信息
	 * @param sureStringId 确定按钮文字
	 * @param surelistener 确定按钮监听事件
	 */
	public static MessageDialog warningDialog(Context context, int strId,
											  int sureStringId, OnClickListener surelistener) {
		if (null == context) {
			return null;
		}
		if (context instanceof Activity) {
			Activity activity = (Activity) context;
			if (activity.isFinishing()) {
				return null;
			}
		}
		MessageDialog msgDialog = new MessageDialog(context);
		msgDialog.setOneBtnStyle();
		msgDialog.setMessage(context.getResources().getString(strId));
		msgDialog.setBtn4Text(sureStringId);
		msgDialog.setBtn4ClickListener(surelistener);
		msgDialog.show();
		return msgDialog;
	}

	/**
	 * @param context      上下文
	 * @param strId        提示信息
	 * @param sureStringId 确定按钮文字
	 * @param surelistener 确定按钮监听事件
	 */
	public static MessageDialog warningDialog(Context context, String strId,
											  int sureStringId, OnClickListener surelistener) {
		if (null == context) {
			return null;
		}
		if (context instanceof Activity) {
			Activity activity = (Activity) context;
			if (activity.isFinishing()) {
				return null;
			}
		}
		MessageDialog msgDialog = new MessageDialog(context);
		msgDialog.setOneBtnStyle();
		msgDialog.setMessage(strId);
		msgDialog.setBtn4Text(sureStringId);
		msgDialog.setBtn4ClickListener(surelistener);
		msgDialog.show();
		return msgDialog;
	}

	/******************* 取消和确定的Dialog *************************/

	/**
	 * 生成选择对话框
	 * 
	 * @param context
	 * @param message
	 *            提示信息
	 * @param sureString
	 *            确定按钮的文字
	 * @param cancelString
	 *            关闭按钮的文字
	 * @param surelistener
	 *            确定按钮监听事件
	 * @param cancellistener
	 *            退出按钮监听事件
	 * @return
	 */
	public static MessageDialog chooseDialog(Context context, String message,
											 String sureString, String cancelString,
											 OnClickListener surelistener, OnClickListener cancellistener) {
		if (null == context) {
			return null;
		}
		if (context instanceof Activity) {
			Activity activity = (Activity) context;
			if (activity.isFinishing()) {
				return null;
			}
		}
		MessageDialog msgDialog = new MessageDialog(context);
		msgDialog.setTwoBtnStyle();
		msgDialog.setMessage(message);
		msgDialog.setBtn1ClickListener(cancellistener);
		msgDialog.setBtn2ClickListener(surelistener);
		msgDialog.setBtn1Text(cancelString);
		msgDialog.setBtn2Text(sureString);
		return msgDialog;
	}



	/**
	 * 选择对话框
	 *  @param context
	 *            上下文
	 * @param message
	 *            提示信息
	 * @param sureString
 *            确定按钮文字
	 * @param surelistener
	 */
	public static MessageDialog chooseDialog(Context context, String message, String sureString, OnClickListener surelistener) {

		if (null == context) {
			return null;
		}
		if (context instanceof Activity) {
			Activity activity = (Activity) context;
			if (activity.isFinishing()) {
				return null;
			}
		}
		MessageDialog msgDialog = new MessageDialog(context);
		msgDialog.setTwoBtnStyle();
		msgDialog.setMessage(message);
		msgDialog.setBtn2ClickListener(surelistener);
		if (!TextUtils.isEmpty(sureString)) {
			msgDialog.setBtn2Text(sureString);
		}
		msgDialog.show();
		return msgDialog;
	}

	/**
	 * 生成选择对话框
	 *  @param context      上下文
	 * @param messageId    提示信息
	 * @param sureStringId 确定按钮文字
	 * @param surelistener 确定按钮监听事件
	 */
	public static MessageDialog chooseDialog(Context context, int messageId,
											 int sureStringId, OnClickListener surelistener) {
		if (null == context) {
			return null;
		}
		if (context instanceof Activity) {
			Activity activity = (Activity) context;
			if (activity.isFinishing()) {
				return null;
			}
		}
		MessageDialog msgDialog = new MessageDialog(context);
		msgDialog.setTwoBtnStyle();
		msgDialog.setMessage(context.getResources().getString(messageId));
		msgDialog.setBtn2ClickListener(surelistener);
		if (!TextUtils.isEmpty(String.valueOf(sureStringId))) {
			msgDialog.setBtn2Text(sureStringId);
		}
		msgDialog.show();
		return msgDialog;
	}


	/**
	 * 带EditText Dialog
	 * 
	 * @param context
	 * @param message
	 *            提示内容
	 * @param sureString
	 *            按钮内容
	 * @param surelistener
	 *            按钮事件
	 * @param title
	 *            标题
	 * @param isPassword
	 *            EditText是否为密码
	 * @return
	 */
	public static MessageDialog editDiaglog(Context context, String message,
											String sureString, OnClickListener surelistener, String title,
											boolean isPassword) {
		if (null == context) {
			return null;
		}
		if (context instanceof Activity) {
			Activity activity = (Activity) context;
			if (activity.isFinishing()) {
				return null;
			}
		}
		MessageDialog msgDialog = new MessageDialog(context);
		msgDialog.setEditTextVisible(true);
		if (isPassword) {
			msgDialog.setEditPassword(true);
		} else {
			msgDialog.setEditPassword(false);
		}
		msgDialog.setTitle(title);
		msgDialog.setTwoBtnStyle();
		msgDialog.setMessage(message);
		msgDialog.setBtn2ClickListener(surelistener);
		if (!TextUtils.isEmpty(sureString)) {
			msgDialog.setBtn2Text(sureString);
		}
		msgDialog.show();
		return msgDialog;
	}



	/**
	 * 生成警告对话框
	 * 
	 * @param context
	 *            上下文
	 * @param strId
	 *            提示信息
	 * @param sureStringId
	 *            确定按钮文字
	 * @param surelistener
	 *            确定按钮监听事件
	 * @param title
	 *            标题
	 */

	public static MessageDialog warningDialog(Context context, int strId,
											  int sureStringId, OnClickListener surelistener, int title) {
		if (null == context) {
			return null;
		}
		if (context instanceof Activity) {
			Activity activity = (Activity) context;
			if (activity.isFinishing()) {
				return null;
			}
		}
		MessageDialog msgDialog = new MessageDialog(context);
		msgDialog.setOneBtnStyle();
		msgDialog.setTitle(context.getResources().getString(title));
		msgDialog.setMessage(context.getResources().getString(strId));
		msgDialog.setBtn4Text(sureStringId);
		msgDialog.setBtn4ClickListener(surelistener);
		msgDialog.show();
		return msgDialog;
	}

	/**
	 * 关闭对话框
	 * 
	 * @param dialog
	 */
	public static void dismissLoadingDialog(Activity activity, Dialog dialog) {
		if(activity != null && !activity.isFinishing()){
			if (dialog != null && dialog.isShowing()) {
				dialog.dismiss();
			}
		}
	}

	/**
	 * 显示DiaLog
	 * 
	 * @param dialog
	 */
	public static void showDialog(Activity activity, Dialog dialog) {
		if (dialog != null) {
			if(activity != null && !activity.isFinishing()){
				dialog.show();
			}
		}
	}
	/**
	 * @param context
	 * @param id
	 */
	public static MessageDialog warningDialog(Context context, int id) {
		if (null == context) {
			return null;
		}
		if (context instanceof Activity) {
			Activity activity = (Activity) context;
			if (activity.isFinishing()) {
				return null;
			}
		}
		MessageDialog msgDialog = new MessageDialog(context);
		msgDialog.setOneBtnStyle();
		msgDialog.setMessage(context.getResources().getString(id));
		msgDialog.show();
		return msgDialog;
	}
	/**
	 * @param context
	 * @param msg
	 */
	public static MessageDialog warningDialog(Context context, String msg) {
		if (null == context) {
			return null;
		}
		if (context instanceof Activity) {
			Activity activity = (Activity) context;
			if (activity.isFinishing()) {
				return null;
			}
		}
		MessageDialog msgDialog = new MessageDialog(context);
		msgDialog.setOneBtnStyle();
		msgDialog.setMessage(msg);
		msgDialog.show();
		return msgDialog;
	}

}
